#Command syntax:
#	The command itself may be a series of letters, or a 3 digit numeric code representing a reply.
#		i.e., PRIVMSG or 401, where 401 is an error meaning that a nick or channel specified does not exist
#	Parameters are seperated from the command by a space(or two)
#		The last parameter starts with a colon and may contain spaces; the others may not.

def parse_command(message)
     ctcp = false
     ctcp_type = nil
	# Split the message into an array seperated by spaces
	split = message.split " ", 2
	command = split[0]
	params_string = split[1]
	last_param = nil
  if params_string
    # If a parameter starts with a colon, it's the last parameter
    params_string.split(" ").each do |param|
      if param =~ /^:/
        last_param = param
        break
      end
    end
    # If there's a last parameter (done so to allow spaces), then seperate the params array so that the last parameter isn't seperated by spaces. Else, just throw the parameters together and return.
    if last_param
      params = params_string.split(" ")[0...params_string.split(" ").index(last_param)]
      params << params_string[params_string.index(last_param)...params_string.length].sub(":", "")
    else
      params = params_string.split(" ")
    end
  end
     if params
          msg = params[-1]
          if msg =~ /^\001/ and msg =~ /\001$/
               ctcp = true
               ctcp_type = msg.split(" ")[0].gsub("\001", "")
               params[-1] = msg.gsub("\001", "")
          end
     end
     return {:command => command, :params => params, :ctcp => ctcp, :ctcp_type => ctcp_type}
end